<?php
// $Id: Expectation.php 25 2010-11-16 05:29:28Z mjohnson60@gmail.com $

require_once('simpletest/expectation.php');

/**
 * Enter description here ...
 * 
 * @author Mark Phillip Johnson
 * @version 1.0
 * @copyright Copyright (c) 2010, markjohnson
 * @license http://www.gnu.org/licenses/gpl.html The GNU General Public License v3.0
 *
 */
class EmptyExpectation extends SimpleExpectation 
{
	/**
	 * Enter description here ...
	 * 
	 * @param unknown_type $value
	 * @param unknown_type $message
	 */
	public function __construct($message = '%s') 
	{
		parent::__construct($message);
	}
	
	/* (non-PHPdoc)
	 * @see SimpleExpectation::test()
	 */
	public function test($value) 
	{
		return empty($value);
	}
	
	/* (non-PHPdoc)
	 * @see SimpleExpectation::testMessage()
	 */
	public function testMessage($compare) 
	{
		if ($this->test($compare)) 
		{
			return "Empty expectation [" . $this->dumper->describeValue($compare) . "]";
		}
		else 
		{
			$value = $compare;
			if (is_array($compare)) 
			{
				$value = array();
			}
			else if (is_bool($compare)) 
			{
				$value = FALSE;
			}
			else if (is_numeric($compare)) 
			{
				$value = 0;
			}
			else if (is_object($compare)) 
			{
				$value = NULL;
			}
			else if (is_string($compare)) 
			{
				$value = '';
			}
			else 
			{
				$value = NULL;
			}
			
			return "Empty expectation fails " . $this->dumper->describeDifference($value, $compare);
		}
	}
}

/**
 * Enter description here ...
 * 
 * @author Mark Phillip Johnson
 * @version 1.0
 * @copyright Copyright (c) 2010, markjohnson
 * @license http://www.gnu.org/licenses/gpl.html The GNU General Public License v3.0
 *
 */
class NotEmptyExpectation extends EmptyExpectation 
{	
	/* (non-PHPdoc)
	 * @see SimpleExpectation::test()
	 */
	public function test($value) 
	{
		return !empty($value);
	}
	
	/* (non-PHPdoc)
	 * @see SimpleExpectation::testMessage()
	 */
	public function testMessage($compare) 
	{
		if ($this->test($compare)) 
		{
			return "Not empty expectation [" . $this->dumper->describeValue($compare) . "]";
		}
		else 
		{
			$value = $compare;
			if (is_array($compare)) 
			{
				$value = array();
			}
			else if (is_bool($compare)) 
			{
				$value = FALSE;
			}
			else if (is_numeric($compare)) 
			{
				$value = 0;
			}
			else if (is_object($compare)) 
			{
				$value = NULL;
			}
			else if (is_string($compare)) 
			{
				$value = '';
			}
			else 
			{
				$value = NULL;
			}
			
			return "Not empty expectation fails " . $this->dumper->describeDifference($value, $compare);
		}
	}
}

/**
 * Enter description here ...
 * 
 * @author Mark Phillip Johnson
 * @version 1.0
 * @copyright Copyright (c) 2010, markjohnson
 * @license http://www.gnu.org/licenses/gpl.html The GNU General Public License v3.0
 *
 */
class ContainsExpectation extends SimpleExpectation 
{
	private $value;
	
	/**
	 * Enter description here ...
	 * 
	 * @param unknown_type $value
	 * @param unknown_type $message
	 */
	public function __construct($value, $message = '%s') 
	{
		parent::__construct($message);
		$this->value = $value;
	}
	
	/* (non-PHPdoc)
	 * @see SimpleExpectation::test()
	 */
	public function test($compare) 
	{
		if (is_array($this->value)) 
		{
			return in_array($this->value, $compare);
		}
		else if (is_object($this->value)) 
		{
			foreach ($this->value as $key => $element) 
			{
				if ($compare->{$key} != $element) 
				{
					return FALSE;
				}
			}
			
			return TRUE;
		}
		else if (is_string($this->value)) 
		{
			return (stripos($this->value, $compare) !== FALSE); 
		}
		
		return FALSE;
	}
	
	/* (non-PHPdoc)
	 * @see SimpleExpectation::testMessage()
	 */
	public function testMessage($compare) 
	{
	if ($this->test($compare)) 
		{
			return "Contains expectation [" . $this->dumper->describeValue($compare) . "]";
		}
		else 
		{			
			return "Contains expectation fails " . $this->dumper->describeValue($compare);
		}
	}
}
